//
//  WJJCalendarMonthSelectorView.m
//  WJJCalendarView
//
//  Created by JunWang on 16/3/7.
//  Copyright (c) 2016年 Insigma Hengtian Software Co.,Ltd  All rights reserved.
//



#import "WJJCalendarMonthSelectorView.h"
#import "UIView+WJJUIViewExt.h"

@implementation WJJCalendarMonthSelectorView

#pragma mark - Initialisation

// Designated initialiser
+ (id)view {
    static UINib *nib;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
    });
    
    NSArray *nibObjects = [nib instantiateWithOwner:nil options:nil];
    for (id object in nibObjects) {
        if ([object isKindOfClass:[self class]]) {
            return object;
        }
    }
    
    return nil;
}


- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self != nil) {
        // Initialise properties
    }
    
    return self;
}


- (void)awakeFromNib {
    [super awakeFromNib];

    // Get a dictionary of localised day names
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit fromDate:[NSDate date]];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"EEE";
    NSMutableDictionary *dayNames = [[NSMutableDictionary alloc] init];
    
    for (NSInteger index = 0; index < 7; index++) {
        NSInteger weekday = dateComponents.weekday - [dateComponents.calendar firstWeekday];
        if (weekday < 0) weekday += 7;
    //截取date星期数据返回string的首字母
        [dayNames setObject:[[formatter stringFromDate:dateComponents.date]substringToIndex:1]  forKey:@(weekday)];
        
        dateComponents.day = dateComponents.day + 1;
        dateComponents = [dateComponents.calendar components:NSCalendarCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit fromDate:dateComponents.date];
    }
    
    // Set the day name label texts to localised day names
    CGFloat margin = self.width / 7;
    for (NSInteger index = 0; index < 7; index++) {
        UILabel *label = _dayLabels[index];
        label.width = margin + 5;
        label.x = index * margin;
        label.text = [[dayNames objectForKey:@(label.tag)] uppercaseString];
        
    }
//    for (UILabel *label in self.dayLabels) {
//        label.text = [[dayNames objectForKey:@(label.tag)] uppercaseString];
//    }
    
    
    
}


#pragma mark - UIView methods

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    if ([self isMemberOfClass:[WJJCalendarMonthSelectorView class]]) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        
        CGContextSetLineWidth(context, 1.0);
        CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:205.0/255.0 alpha:1.0].CGColor);
        CGContextMoveToPoint(context, 0.0, self.bounds.size.height - 0.5);
        CGContextAddLineToPoint(context, self.bounds.size.width - 0.5, self.bounds.size.height - 0.5);
        CGContextStrokePath(context);
        
        CGContextRestoreGState(context);
    }
}


@end
